package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.apache.dubbo.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
@Slf4j
@Repository
public class CouponRepositoryImpl implements CouponRepository {
    @Autowired
    private CouponMapper couponMapper;

    @Override
    public List<CouponVO> selectList(QueryWrapper<CouponVO> queryWrapper) {
        Wrapper<CouponPO>cPO=new QueryWrapper<>();
        cPO=convertVoWrapperToPoWrapper(queryWrapper);
        log.debug("仓储层cPO:{}",cPO);
        List<CouponPO> couponPOS = couponMapper.selectList(cPO);
        log.debug("仓储层couponPOS:{}",couponPOS);
        List<CouponVO>couponVOS=convertPoListToVoList(couponPOS);
        log.debug("仓储层couponVOS:{}",couponVOS);
        return couponVOS;
    }






    /**
     * 将 QueryWrapper<CouponVO> 转换为 QueryWrapper<CouponPO>
     */
    public QueryWrapper<CouponPO> convertVoWrapperToPoWrapper(QueryWrapper<CouponVO> voWrapper) {
        QueryWrapper<CouponPO> poWrapper = new QueryWrapper<>();

        // 1. 复制实体对象（如果存在）
        if (voWrapper.getEntity() != null) {
            CouponVO voEntity = voWrapper.getEntity();
            CouponPO poEntity = new CouponPO();
            BeanUtils.copyProperties(voEntity, poEntity);
            poWrapper.setEntity(poEntity);
        }
        // 2. 复制 SELECT 字段（如果设置了）
        if (StringUtils.isNotBlank(voWrapper.getSqlSelect())) {
            poWrapper.select(voWrapper.getSqlSelect());
        }
        // 3. 复制 SQL 条件
        String customSqlSegment = voWrapper.getCustomSqlSegment();
        if (StringUtils.isNotBlank(customSqlSegment)) {
            // 去掉开头的 "WHERE "（如果存在）
            if (customSqlSegment.startsWith("WHERE ")) {
                customSqlSegment = customSqlSegment.substring(6);
            }
            poWrapper.apply(customSqlSegment);
        }
        return poWrapper;
    }
    /**
     * 将 List<CouponVO> 转换为 List<CouponPO>
     */
    public List<CouponVO> convertPoListToVoList(List<CouponPO> poList) {
        if (poList == null) {
            return new ArrayList<>();
        }

        return poList.stream()
                .map(this::convertPoToVo)
                .collect(Collectors.toList());
    }

    /**
     * 单个 PO 转 VO 的辅助方法
     */
    private CouponVO convertPoToVo(CouponPO po) {
        if (po == null) {
            return null;
        }

        CouponVO vo = new CouponVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

}
